package com.genesis.yunnanji.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.genesis.yunnanji.R;
import com.genesis.yunnanji.bean.LoginBean;
import com.genesis.yunnanji.bean.PersonBean;
import com.genesis.yunnanji.config.GenesisApiConfig;
import com.genesis.yunnanji.fragment.PasswordLogin;
import com.genesis.yunnanji.view.DataLoadingDialog;
import com.google.gson.Gson;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;


import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.Map;

import es.dmoral.toasty.Toasty;



public class Account extends BaseActivity {
    @ViewInject(R.id.tb_account_toolbar)
    private Toolbar mToolbar;
    @ViewInject(R.id.ll_account_bindingphone)
    private RelativeLayout rlBindPhone;
    @ViewInject(R.id.ll_account_bindingEmail)
    private RelativeLayout rlBindEmail;
    @ViewInject(R.id.ll_account_password)
    private RelativeLayout rlChangePwd;
    @ViewInject(R.id.ll_account_qq)
    private RelativeLayout rlBindQQ;
    @ViewInject(R.id.ll_account_bindingWX)
    private RelativeLayout rlBindWeixin;
    @ViewInject(R.id.tv_account_passwordrevise)
    private TextView tvPwdStatus;
    @ViewInject(R.id.tv_account_bindphonestatus)
    private TextView tvMobile_status;
    @ViewInject(R.id.tv_account_bindqqstatus)
    private TextView connect_qq;
    @ViewInject(R.id.tv_account_bindWXstatus)
    private TextView connect_weixin;
    private PersonBean personBean;
    private int loginselect;
    private DataLoadingDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState, R.layout.activity_account);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        dialog = new DataLoadingDialog(context);
        mToolbar.setTitle("");
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        getPersonInfo();
    }

    private Intent intent;

    @Event(value = {R.id.ll_account_bindingphone, R.id.ll_account_bindingEmail, R.id.ll_account_password, R.id.ll_account_qq, R.id.ll_account_bindingWX}, type = View.OnClickListener.class)
    private void QQWXlogin0(View v) {

        switch (v.getId()) {
            case R.id.ll_account_bindingphone:
                if (personBean.getResult().getInfo().getMobile_status().equals("1")) {
                    Toasty.info(context, "您已经绑定过手机", Toast.LENGTH_SHORT).show();
                } else {
                    intent = new Intent(context, BindPhone.class);
                    startActivity(intent);
                }

                break;
            case R.id.ll_account_bindingEmail:
                intent = new Intent(context, BindEmail.class);
                startActivity(intent);
                break;
            case R.id.ll_account_password:
                intent = new Intent(context, ChangePwd.class);
                intent.putExtra("pwd_status", genesisUtils.getIssetpwd());
                startActivity(intent);
                break;
            case R.id.ll_account_qq:
                if (personBean.getResult().getInfo().getConnect_qq() == 1) {
                    Toasty.info(context, "您已经绑定过QQ", Toast.LENGTH_SHORT).show();
                } else {
                    loginselect = 0;
                    dialog.show();
                    AuthLogin(SHARE_MEDIA.QQ);
                }
                break;
            case R.id.ll_account_bindingWX:
                if (personBean.getResult().getInfo().getConnect_weixin() == 1) {
                    Toasty.info(context, "您已经绑定过微信", Toast.LENGTH_SHORT).show();
                } else {
                    loginselect = 1;
                    dialog.show();
                    AuthLogin(SHARE_MEDIA.WEIXIN);
                }
                break;


        }

    }

    @Override
    protected void setUpView() {

    }


    private void getPersonInfo() {
        RequestParams params = new RequestParams(GenesisApiConfig.HOST + GenesisApiConfig.PERSON_INFO);
        params.addBodyParameter("__apptoken__", genesisUtils.getAppToken());
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                personBean = gson.fromJson(result, PersonBean.class);
                if (personBean.getResult().getInfo().getConnect_qq() == 1) {
                    connect_qq.setText("已绑定");
                } else {
                    connect_qq.setText("未绑定");
                }
                if (personBean.getResult().getInfo().getConnect_weixin() == 1) {
                    connect_weixin.setText("已绑定");
                } else {
                    connect_weixin.setText("未绑定");
                }
                if (personBean.getResult().getInfo().getMobile_status().equals("1")) {
                    tvMobile_status.setText("已绑定");
                } else {
                    tvMobile_status.setText("未绑定");
                }
                genesisUtils.setIssetpwd(personBean.getResult().getInfo().getIssetpwd());
                if (personBean.getResult().getInfo().getIssetpwd() == 0) {
                    tvPwdStatus.setText("设置");
                } else if (personBean.getResult().getInfo().getIssetpwd() == 1) {
                    tvPwdStatus.setText("修改");
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    public void AuthLogin(SHARE_MEDIA platform) {

        UMShareAPI.get(context).getPlatformInfo(this, platform, new UMAuthListener() {
            @Override
            public void onStart(SHARE_MEDIA share_media) {

            }

            @Override
            public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
                QQLogin(map.get("uid"), map.get("name"), map.get("gender"), map.get("iconurl"), "");
            }

            @Override
            public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
                Toast.makeText(getApplicationContext(), "Authorize fail", Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }

            @Override
            public void onCancel(SHARE_MEDIA share_media, int i) {
                Toast.makeText(getApplicationContext(), "Authorize CANCEL", Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        });

    }


    private void QQLogin(String open_id, String nickname, String sex, String photo, String birthday) {
        String url = "";
        if (loginselect == 0) {
            url = GenesisApiConfig.HOST + GenesisApiConfig.QQ_LOGIN;
        } else if (loginselect == 1) {
            url = GenesisApiConfig.HOST + GenesisApiConfig.WEIXIN_LOGIN;
        }
        RequestParams params = new RequestParams(url);
        params.addBodyParameter("openid", open_id);
        params.addBodyParameter("nickname", nickname);
        if (sex.equals("男")) {
            params.addBodyParameter("sex", 1 + "");
        } else if (sex.equals("女")) {
            params.addBodyParameter("sex", 2 + "");
        } else {
            params.addBodyParameter("sex", 0 + "");
        }
        params.addBodyParameter("photo", photo);
        params.addBodyParameter("birthday", birthday);
        params.addBodyParameter("__apptoken__", genesisUtils.getAppToken());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.e("bind", result);
                Gson gson = new Gson();
                LoginBean loginBean = gson.fromJson(result, LoginBean.class);
                if (loginBean.getStatus() == 200) {
                    Intent intent = new Intent();
                    intent.setAction(PasswordLogin.LOGIN_STATUS);
                    intent.putExtra("msg", "login");
                    context.sendBroadcast(intent);
                    getPersonInfo();
                    Toasty.success(context, loginBean.getMessage(), Toast.LENGTH_SHORT).show();

                } else {
                    Toasty.error(context, loginBean.getMessage(), Toast.LENGTH_SHORT).show();
                    deleteAuth(SHARE_MEDIA.QQ);
                    deleteAuth(SHARE_MEDIA.WEIXIN);
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                dialog.dismiss();
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    public void deleteAuth(SHARE_MEDIA share_media) {
        UMShareAPI.get(context).deleteOauth(this, share_media, new UMAuthListener() {
            @Override
            public void onStart(SHARE_MEDIA share_media) {

            }

            @Override
            public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
            }

            @Override
            public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {

            }

            @Override
            public void onCancel(SHARE_MEDIA share_media, int i) {

            }
        });
    }

}
